package com.yootk.common.validation.annotation.handle;

import com.yootk.common.validation.annotation.RegexValidator;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 正则校验处理类
 */
public class RegexConstraintValidator implements ConstraintValidator<RegexValidator, Object> {

    private String regexExpression;     // 保存正则表达式

    @Override
    public void initialize(RegexValidator constraintAnnotation) {
        this.regexExpression = constraintAnnotation.pattern();  // 获取配置表达式
    }

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        if (value == null) {
            return false;
        }
        return value.toString().matches(this.regexExpression);  // 数据校验
    }
}
